package com.xydtech.ecard.custgroup.service.impl;

import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.ecard.config.mapper.EcardApiSysConfigMapper;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerOrganizeTask;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerOrganizeVisit;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerOrganizeTaskMapper;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerOrganizeVisitMapper;
import com.xydtech.ecard.custgroup.service.IEcardApiCustomerOrganizeVisitService;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.mapper.EcardApiUserMapper;
import com.xydtech.system.domain.SysConfig;
import com.xydtech.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 电子名片社团组织拜访记录Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-11-29 上午11:24
 */
@Service
@Slf4j
public class EcardApiCustomerOrganizeVisitServiceImpl implements IEcardApiCustomerOrganizeVisitService {

    @Autowired
    private EcardApiCustomerOrganizeVisitMapper ecardCustomerOrganizeVisitMapper;
    @Autowired
    private EcardApiCustomerOrganizeTaskMapper ecardCustomerOrganizeTaskMapper;
    @Autowired
    private EcardApiSysConfigMapper ecardApiSysConfigMapper;
    @Autowired
    private EcardApiUserMapper ecardApiUserMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询电子名片社团组织拜访记录列表
     * @param ecardCustomerOrganizeVisit
     * @return
     */
    @Override
    public List<EcardApiCustomerOrganizeVisit> getOrganizeVisitList(EcardApiCustomerOrganizeVisit ecardCustomerOrganizeVisit) {
        return ecardCustomerOrganizeVisitMapper.getOrganizeVisitList(ecardCustomerOrganizeVisit);
    }

    /**
     * 查询电子名片社团组织拜访记录
     * @param id
     * @return
     */
    @Override
    public EcardApiCustomerOrganizeVisit selectEcardCustomerOrganizeVisitById(Long id) {
        return ecardCustomerOrganizeVisitMapper.selectEcardCustomerOrganizeVisitById(id);
    }

    /**
     * 根据社团组织ID查询最近一次拜访时间
     * @param organizeId
     * @return
     */
    @Override
    public String getOrganizeLastVisitTime(Long organizeId) {
        return ecardCustomerOrganizeVisitMapper.getOrganizeLastVisitTime(organizeId);
    }

    /**
     * 保存社团组织拜访信息
     * @param ecardCustomerOrganizeVisit
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEcardCustomerOrganizeVisit(EcardApiCustomerOrganizeVisit ecardCustomerOrganizeVisit) {
        try {
            int updateCount = ecardCustomerOrganizeVisitMapper.insertEcardCustomerOrganizeVisit(ecardCustomerOrganizeVisit);

            //判断是否存在代办任务
            List<EcardApiCustomerOrganizeTask> organizeTaskList = ecardCustomerOrganizeTaskMapper.getOrganizeTaskListByOrganizeId(ecardCustomerOrganizeVisit.getOrganizeId());
            if (organizeTaskList.size() > 0) {
                for (int i = 0; i < organizeTaskList.size(); i++) {
                    EcardApiCustomerOrganizeTask customerOrganizeTask = organizeTaskList.get(i);
                    //存在就更新任务表数据
                    ecardCustomerOrganizeTaskMapper.updateOrganizeTaskSts(customerOrganizeTask);
                }
            }
            //查询参数配置-拜访任务定时天数
            EcardApiUserInfo userInfo = ecardApiUserMapper.getUserInfo(String.valueOf(ecardCustomerOrganizeVisit.getCreatorId()));
            SysConfig config = new SysConfig();
            config.setConfigKey("ecard_merchant_task_days");
            //查询改为多法人，归属的法人机构
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
            if (dept != null){
                config.setDeptId(dept.getDeptId());
            }
            SysConfig sysConfig = ecardApiSysConfigMapper.selectConfig(config);
            //判断下次回访日期是否小于拜访任务定时天数，小于则创建任务
            DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
            //开始日期
            String star = String.format("%tF", new Date());
            //结束时间
            String endDay = String.format("%tF", ecardCustomerOrganizeVisit.getReminderDate());
            int days = 0;
            try {
                days = DateUtils.differentDaysByMillisecond(dft.parse(star), dft.parse(endDay));
            } catch (ParseException e) {
                throw new RuntimeException();//抛出新的异常
            }
            if (days <= Integer.valueOf(sysConfig.getConfigValue())) {
                EcardApiCustomerOrganizeTask organizeTask = new EcardApiCustomerOrganizeTask();
                organizeTask.setOrganizeId(ecardCustomerOrganizeVisit.getOrganizeId());
                organizeTask.setCmRegistId(ecardCustomerOrganizeVisit.getCreatorId());
                organizeTask.setDeptId(userInfo.getDeptId());//创建人机构id
                organizeTask.setCorpId(dept.getDeptId());//法人机构id
                organizeTask.setEndDate(ecardCustomerOrganizeVisit.getReminderDate());
                organizeTask.setTaskSts("0");
                ecardCustomerOrganizeTaskMapper.insertEcardCustomerOrganizeTask(organizeTask);
            }
            return updateCount;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw e;
        }
    }

    /**
     * 查询所有的拜访记录
     * @param ecardApiCustomerOrganizeVisit
     * @return
     */
    @Override
    public List<EcardApiCustomerOrganizeVisit> getAllVisitList(EcardApiCustomerOrganizeVisit ecardApiCustomerOrganizeVisit) {
        return ecardCustomerOrganizeVisitMapper.getAllVisitList(ecardApiCustomerOrganizeVisit);
    }
}
